//
//  UIViewController+SVPHD.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 HZMengLiao. All rights reserved.
//

#import "UIViewController+THHud.h"
#import "MBProgressHUD/MBProgressHUD.h"
#include <objc/runtime.h>
static const void *NeiquanHttpRequestHUDKey = &NeiquanHttpRequestHUDKey;
static const void *LMHttpRequestHUDKey = &LMHttpRequestHUDKey;
@implementation UIViewController (SVPHD)
- (void)showErrorToastMsg:(NSString*)message{
    if (message.length <= 0) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = message;
        hud.margin = 15.f;
        //此属性设置为NO 弹出hud 时才能点击屏幕其他地方
        hud.userInteractionEnabled = NO;
        hud.minSize = CGSizeMake(100, 45);
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.color =  [[UIColor blackColor] colorWithAlphaComponent:0.55];
        hud.label.textColor=[UIColor whiteColor];
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:1.5];
        [self setHUD:hud];
    });
}
- (void)showErrorToastMsg:(NSString*)message offset:(CGFloat)offset{
    if (message.length <= 0) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:window?window:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = message;
        hud.margin = 15.f;
        //此属性设置为NO 弹出hud 时才能点击屏幕其他地方
        hud.userInteractionEnabled = NO;
        hud.offset  = CGPointMake(0,window? 0:offset);
        hud.minSize = CGSizeMake(100, 45);
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.color =  [[UIColor blackColor] colorWithAlphaComponent:0.55];
        hud.label.textColor=[UIColor whiteColor];
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:1.5];
        [self setHUD:hud];
    });
}
- (void)showLoadMessage:(NSString *)aMessage
{
    if (aMessage.length <= 0) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self hideProgressView];
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = aMessage;
        hud.label.numberOfLines = 0;
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.55];
        hud.bezelView.layer.cornerRadius = 20;
        hud.bezelView.layer.masksToBounds = YES;
        hud.label.textColor = [UIColor whiteColor];
        hud.label.font = [UIFont fontWithName:FONT_REGULAR size:15];
        hud.minSize = CGSizeMake(100, 100);
        hud.square = YES;
        hud.animationType = MBProgressHUDAnimationZoomOut; //动画
        hud.removeFromSuperViewOnHide = YES;
        [hud showAnimated:NO];
        [self setHUD:hud];
    });
}
- (void)showLoading
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self hideProgressView];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.removeFromSuperViewOnHide = YES;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.55];
        hud.minSize = CGSizeMake(100, 100);
        [self setHUD:hud];
    });
}
//- (void)showErrorMessage:(NSString *)aMessage
//{
//    [self showHUDImageName :@"tmall_notice_failed" Message:aMessage];
//}
//- (void)showNetErrorMessage:(NSString *)aMessage
//{
//    [self showHUDImageName:@"tmall_notice_info" Message:aMessage];
//}
//- (void)showSuccessMessage:(NSString *)aMessage
//{
//    [self showHUDImageName:@"lm_Toast_Success" Message:aMessage];
//}
//- (void)showSuccessMessage:(NSString *)aMessage complete:(void(^)(void))completeBlock{
//
//    [self showHUDImageName:@"lm_Toast_Success" Message:aMessage];
//    if (completeBlock) {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            completeBlock();
//        });
//    }
//}
- (void)showHUDImageName:(NSString *)imagestring Message:(NSString *)aMessage{
    if (aMessage.length <= 0) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self hideProgressView];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.label.text = aMessage;
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imagestring]];
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.removeFromSuperViewOnHide = YES;
        hud.bezelView.layer.cornerRadius = 10.0f;
        hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.55];
        hud.label.textColor = [UIColor whiteColor];
        hud.label.font = [UIFont fontWithName:FONT_REGULAR size:14];
        hud.minSize = CGSizeMake(100, 100);
        hud.square = YES;
        [hud hideAnimated:YES afterDelay:1.5];
    });
}
#pragma mark ----------移除MBProgress
- (void)hideProgressView{
    [[self HUD]  hideAnimated:YES afterDelay:0.0];
}
- (MBProgressHUD *)HUD{
    return objc_getAssociatedObject(self, NeiquanHttpRequestHUDKey);
}
- (void)setHUD:(MBProgressHUD *)HUD{
    objc_setAssociatedObject(self, NeiquanHttpRequestHUDKey, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
